<?php
class questmanageController extends AController {
	private $tableName = 'QUESTION_OPTIONS';
	
	public function indexAction(){
		$qid = $this->request->getGetParameter('id');
		$questInfo = $this->getQuestInfoById($qid);
		if($questInfo['QTYPE']>2){
			$this->alert('选择题才用选项管理，其它项不用');exit;
		}
		$opt = $this->getOptInfo($qid);
		$this->qid = $qid;
		$this->opt = $opt;
		$this->quest = $questInfo;
		return $this->render();
	}
	
	public function editAction(){
		$id = $this->request->getGetParameter('pid');
		$backid = $this->request->getGetParameter('id');
		if(isset($_POST['opt_edit'])){
			$key = $this->request->getPostParameter('key');
			$opt['opt_name'] = $name = $this->request->getPostParameter('opt_name');
			$opt['opt_value'] = $val = $this->request->getPostParameter('opt_val');
			$opt['allow_input'] = $input = $this->request->getPostParameter('opt_input');
			$opt['sort_no'] = $order = $this->request->getPostParameter('opt_order');
			if($key == md5($name.$val.$input.$order)){
				$this->alert('您没有修改任何值');exit;
			}
			if($this->editOptionById($id,$opt)){
				$this->alert('修改成功','/index.php?c=questmanage&a=index&id='.$backid);exit;
			}
			$this->alert('系统繁忙，请重试');exit;
		}
		
		$optInfo = $this->getOptInfoById($id);
		$key = md5($optInfo['OPT_NAME'].$optInfo['ALLOW_INPUT'].$optInfo['OPT_VALUE'].$optInfo['SORT_NO']);
		$this->optinfo = $optInfo;
		$this->key = $key;
		return $this->render();
	}
	
	public function addAction(){
		if(isset($_POST['opt_add'])){
			$opt['opt_name']= $name = $this->request->getPostParameter('opt_name');
			$opt['opt_value'] = $val = $this->request->getPostParameter('opt_val');
			$opt['ALLOW_INPUT'] = $input = $this->request->getPostParameter('opt_input');
			$opt['sort_no'] = $input = $this->request->getPostParameter('opt_order');
			$opt['q_id'] = $this->request->getGetParameter('id');
			$opt['UNIT_ID'] = $this->unid;
			$opt['STATE'] = 1;
			if($name.$val.$input == $input || $name.$val.$input == $val.$input){
				$this->alert('请输入选项名称');exit;
			}
			if($this->addOptionInfo($opt)){
				$this->alert('添加成功');exit;
			}
			$this->alert('系统繁忙，请重试');exit;
		}
		return $this->render();
	}
	
	public function delAction(){
		$id = $this->request->getGetParameter('id');
		if($this->delOptionById($id)){
			$this->alert('删除成功');exit;
		}
		$this->alert('系统繁忙，请重试');exit;
	}
	function getOptInfo($id){ //查询指定题目的所有选项
		$where = 'state=1 and q_id='.$id;
		return JYDB::findAll($this->tableName,$where,'*','allow_input desc,opt_id');
	}
	function getOptInfoById($id){ //查询指定ID的选项信息
		$where = 'state=1 and opt_id='.$id;
		return JYDB::find($this->tableName,$where);
	}
	function getQuestInfoById($id){ //得到指定ID的题目信息
		$where = 'q_id='.$id;
		$data = JYDB::find('QUESTION',$where,'TITLE,QTYPE');
		return $data;
	}
	function editOptionById($id,$arr){
		$where = 'opt_id='.$id;
		return JYDB::update($this->tableName,$where,$arr);
	}
	function delOptionById($id){
		$where = 'opt_id='.$id;
		return JYDB::update($this->tableName,$where,array('state'=>0));
	}
	function addOptionInfo($arr){ //添加题目选项内容
//		var_dump($arr);exit;
		return JYDB::create($this->tableName,$arr,'OPT_ID');
	}
}